package com.hz.cheetahserver.config;

import com.hz.cheetah.hub.zookeeper.ZooKeeperServiceRegistry;
import com.hz.cheetah.server.CheetahServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by yangchuan on 17/2/16.
 */
@Configuration
@EnableConfigurationProperties({ServerConfig.class})
public class ServerAutoConfig {

    @Autowired
    private ServerConfig myConfig;

    @Autowired
    @Bean
    @ConditionalOnMissingBean
    public CheetahServer cheetahServer(ZooKeeperServiceRegistry serviceRegistry) {
        return new CheetahServer(myConfig.getService(), serviceRegistry);
    }


    @Bean
    @ConditionalOnMissingBean
    public ZooKeeperServiceRegistry zooKeeperServiceRegistry() {
        ZooKeeperServiceRegistry result = new ZooKeeperServiceRegistry();
        result.setZkAddress(myConfig.getAddress());
        result.init();
        return result;
    }



}
